// dfs

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <cmath>

using namespace std;

class Solution {
public:
    int minPath(vector<unordered_set<int>> &edges, int u, int v) {
        if (u == v)return 0;
        unordered_set<int> visited;
        queue<int> q;
        q.push(u);
        visited.emplace(u);
        int pathLength = 1;
        int curr_cnt = 1;
        while (!q.empty() && curr_cnt > 0) {
            int i = curr_cnt;
            curr_cnt = 0;
            for (; i > 0; --i) {
                int x = q.front();
                q.pop();
                for (int y : edges[x]) {
                    if (y == v)
                        return pathLength;
                    if (visited.count(y))
                        continue;
                    q.push(y);
                    visited.emplace(y);
                    ++curr_cnt;
                }
            }
            ++pathLength;
        }
        return -1;
    }
};

int main() {
    int N, M, u, v;
    cin >> N >> M >> u >> v;
    vector<unordered_set<int>> edges(N);
    while (M--) {
        int x, y;
        cin >> x >> y;
        edges[x - 1].emplace(y - 1);
        edges[y - 1].emplace(x - 1);
    }
    cout << Solution().minPath(edges, u - 1, v - 1) << endl;
    // system("pause");
    return 0;
}